import constant as constant
from Bay import Bay
from Berth import Berth
from Block import Block
from Box import Box
from Task import Task
from Truck import Truck
import random

len_b=constant.len_b
berth_list = constant.berth_list
bay_id=0
bay_list = constant.bay_list
truck_list = constant.truck_list
# box_list 二维列表 box_list[0][...] 表示type=0的箱子
box_list = constant.box_list
#存放所有箱子信息的元组（type，id）
box_type_id_list = constant.box_type_id_list
#存放 任务箱子信息的元组（type，id）
task_box_typeid_list=constant.task_box_typeid_list
task_list = constant.task_list
block_list=constant.block_list


def init_berth():
    len_b = constant.len_b
    qc_x = [len_b * 4, len_b * 8, len_b * 12, len_b * 20, len_b * 24, len_b * 28]
    for i in range(len(qc_x)):
        berth = Berth(i, qc_x[i], 0,i)
        berth_list.append(berth)
    print("6 个岸吊位置初始化成功！")


def init_truck():
    for i in range(constant.num_truck):
        truck = Truck(i, 0)
        truck_list.append(truck)
    print(constant.num_truck,"个卡车初始化成功！")


def init_box():
    temp = []
    for i in range(constant.box0_num):
        box = Box(i, 0)
        temp.append(box)
        box_type_id_list.append((0, i))
    box_list.append(temp)
    temp = []
    for i in range(constant.box1_num):
        box = Box(i, 1)
        temp.append(box)
        box_type_id_list.append((1, i))
    box_list.append(temp)
    temp = []
    for i in range(constant.box2_num):
        box = Box(i, 2)
        temp.append(box)
        box_type_id_list.append((1, i))
    box_list.append(temp)
    print(constant.box0_num+constant.box1_num+constant.box2_num,"个箱子初始化成功！")
    # for boxs in box_list:
    #     print(id(boxs))
    #     for i in boxs:
    #         print(id(i),end=" ")
    #     print(" ")


def init_task():
    for i in range(constant.num_task):
        task = Task(i)
        id_box = box_type_id_list.pop(random.randint(0, len(box_type_id_list) - 1))
        task_box_typeid_list.append(id_box)
        box_temp=box_list[id_box[0]][id_box[1]]
        box_temp.id_block=random.randint(0,constant.num_block-1)
        task.add_box(box_temp)
        id_box = box_type_id_list.pop(random.randint(0, len(box_type_id_list) - 1))
        task_box_typeid_list.append(id_box)
        box_temp = box_list[id_box[0]][id_box[1]]
        box_temp.id_block = random.randint(0, constant.num_block - 1)
        task.add_box(box_temp)
        task_list.append(task)
    print(constant.num_task,"个任务初始化成功！")

# 在 init_block 中调用
def init_bay(id_block,x,y,id_block_list):
    x+=len_b/2
    global bay_id
    for i in range(constant.bay_numof_block):
        bay=Bay(bay_id,id_block,x,y)
        block_list[id_block_list].add_bay(bay)
        bay_list.append(bay)
        bay_id+=1
        x+=len_b
    #print("第 {} 个block的bay位初始化成功！".format(id_block))

def init_block():
    y=len_b*3
    point_id=6
    block_id=0
    for i in range(5):
        x=len_b
        for j in range(3):
            block=Block(block_id,x,y,point_id)
            block_list.append(block)
            init_bay(block_id,x,y,len(block_list)-1)
            block=Block(block_id+3,x,y+len_b,point_id+4)
            block_list.append(block)
            init_bay(block_id+3,x,y+len_b,len(block_list)-1)
            x=x+len_b*11
            block_id+=1
            point_id+=1
        block_id+=3
        point_id+=1
        y+=len_b*2.5
    
    y=len_b*16
    for i in range(5):
        x=len_b
        for j in range(3):
            block=Block(block_id,x,y,point_id)
            block_list.append(block)
            init_bay(block_id,x,y,len(block_list)-1)
            block=Block(block_id+3,x,y+len_b,point_id+4)
            block_list.append(block)
            init_bay(block_id+3,x,y+len_b,len(block_list)-1)
            x=x+len_b*11
            block_id+=1
            point_id+=1
        block_id+=3
        point_id+=1
        y+=len_b*2.5
    
    print(constant.num_block," 个箱区初始化成功！")
    #对block_list 根据 block_id 排序
    block_list.sort(key= lambda x:x.id)

def init_data():
    init_berth()
    init_truck()
    init_box()
    init_block()
    init_task()
    # print(len(bay_list))
    # print(len(block_list))
    # for i in range(0,100):
    #     print(bay_list[i].id,bay_list[i].id_block,bay_list[i].num_free_place,bay_list[i].x,bay_list[i].y)

if __name__ == "__main__":
    init_data()
    print(len(berth_list))
    print(len(box_list))
    print(len(box_type_id_list))
    print(len(truck_list))
    print(len(bay_list))
    print(len(block_list))
    print(len(task_list))
